<?php
namespace src\logic;
use \classlib\core as C;
class lrobots {
	private static function __init__() {
		$conf   = C\config::load('im', false);
		if (!isset($conf['robots'])) {
			return false;
		}
		list($host, $port) = explode(':', $conf['robots']);
		$socket = new \classlib\imsdk\socket($host, $port);
		if (!$socket->connect()) {
			\classlib\logger::inst()->write('robots socket connect failure');
			return false;
		}
		return $socket;
	}
	
	//通知机器人有变动了
	public static function notify() {
		$socket = self::__init__();
		if (!$socket) {
			return;
		}
		$param = array('robot_changed'=>1);
		$proc  = new \classlib\imsdk\binproc();
		$json  = json_encode($param);
		$len   = strlen($json);
		$pad   = str_repeat(chr(0), 25);
		$data  = '3'.$proc->write_int($len).$pad.$json;
		$size  = $socket->send($data, $len+30);
		if ($size != 0) {
			\classlib\logger::inst()->write('robots host sends failure');
			return false;
		}
		$str  = $socket->revc(30);
		$proc->__init__($str);
		$code = $proc->read_byte();
		$len  = $proc->read_int();
		if ($len < 1) {
			\classlib\logger::inst()->write('robots host reads failure');
			return false;
		}
		$data = $socket->revc($len);
		$data = json_decode($data, true);
		return $data;
	}
	
	//重置直播间机器人
	public static function reset($sid) {
		$socket = self::__init__();
		if (!$socket) {
			return;
		}
		$param = array('reset_studioid'=>$sid);
		$proc  = new \classlib\imsdk\binproc();
		$json  = json_encode($param);
		$len   = strlen($json);
		$pad   = str_repeat(chr(0), 25);
		$data  = '4'.$proc->write_int($len).$pad.$json;
		$size  = $socket->send($data, $len+30);
		if ($size != 0) {
			\classlib\logger::inst()->write('robots host sends failure');
			return false;
		}
		$str  = $socket->revc(30);
		$proc->__init__($str);
		$code = $proc->read_byte();
		$len  = $proc->read_int();
		if ($len < 1) {
			\classlib\logger::inst()->write('robots host reads failure');
			return false;
		}
		$data = $socket->revc($len);
		$data = json_decode($data, true);
		return $data;
	}
	
	//添加机器人接口
	public static function add($studioid, $count=0, $time=3) {
		$socket = self::__init__();
		if (!$socket) {
			return;
		}
		if ($count < 1) {
			$count = mt_rand(1, 10);
		}
		$param = array('StudioId'=>$studioid, 'SetRobCount'=>$count, 'MaxTime'=>$time);
		$proc  = new \classlib\imsdk\binproc();
		$json  = json_encode($param);
		$len   = strlen($json);
		$pad   = str_repeat(chr(0), 25);
		$data  = '1'.$proc->write_int($len).$pad.$json;
		$size  = $socket->send($data, $len+30);
		if ($size != 0) {
			\classlib\logger::inst()->write('robots host sends failure');
			return false;
		}
		$str  = $socket->revc(30);
		$proc->__init__($str);
		$code = $proc->read_byte();
		$len  = $proc->read_int();
		if ($len < 1) {
			\classlib\logger::inst()->write('robots host reads failure');
			return false;
		}
		$data = $socket->revc($len);
		$data = json_decode($data, true);
		return $data;
	}
	
	//添加机器人接口
	public static function del($studioid) {
		$socket = self::__init__();
		if (!$socket) {
			return;
		}
		$param= array('StudioId'=>$studioid);
		$proc = new \classlib\imsdk\binproc();
		$json = json_encode($param);
		$len  = strlen($json);
		$pad  = str_repeat(chr(0), 25);
		$data = '2'.$proc->write_int($len).$pad.$json;
		$size = $socket->send($data, $len+30);
		if ($size != 0) {
			\classlib\logger::inst()->write('robots host sends failure');
			return false;
		}
		$str  = $socket->revc(30);
		$proc->__init__($str);
		$code = $proc->read_byte();
		$len  = $proc->read_int();
		if ($len < 1) {
			\classlib\logger::inst()->write('robots host reads failure');
			return false;
		}
		$data = $socket->revc($len);
		$data = json_decode($data, true);
		return $data;
	}
}